<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHeader @add="addList" :arr="getList"></TodoHeader>
    <TodoMain :arr="getList" @delete="delList"></TodoMain>
    <TodoFooter :arr="getList" @checked="get" @delete="clear"></TodoFooter>
  </section>
</template>

<script>
// 1.0 样式引入
import "./styles/base.css";
import "./styles/index.css";

import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";

export default {
  data() {
    return {
      list: JSON.parse(localStorage.getItem("List")) || [],
      curtype: "all",
      bool: "",
    };
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  methods: {
    addList(val) {
      if (val !== "") {
        let id =
          this.list.length > 0 ? this.list[this.list.length - 1].id + 1 : 1;
        let arr = {
          id: id,
          name: val,
          isDone: false,
        };
        this.list.push(arr);
      } else {
        return;
      }
    },
    delList(index) {
      this.list.splice(index, 1);
    },
    get(type) {
      console.log(type);
      this.curtype = type;
    },
    clear() {
      this.list = this.list.filter((v) => {
        return !v.isDone;
      });
    },
  },
  watch: {
    list: {
      deep: true,
      immediate: true,
      handler(newval) {
        localStorage.setItem("List", JSON.stringify(newval));
      },
    },
  },
  computed: {
    getList() {
      if (this.curtype === "yes") {
        return this.list.filter((item) => {
          return item.isDone;
        });
      } else if (this.curtype === "no") {
        return this.list.filter((v) => {
          return !v.isDone;
        });
      } else {
        return this.list;
      }
    },
  },
};
</script>